<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <meta charset="UTF-8">
    <title>客户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" th:href="@{/favicon.ico}">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/layui_ext/dtree/dtree.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/layui_ext/dtree/font/dtreefont.css}" media="all"/>
    <style>
        .layui-elem-quote .layui-form .layui-form-item .layui-input-inline {
            width: 150px;
        }
    </style>
</head>
<body class="childrenBody" sec:authorize="hasPermission('/admin/user/customerList',null)">
<!-- 查询条件开始 -->
<blockquote class="layui-elem-quote">
    <form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 80px">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input"  style="width: 150px">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 80px">账号名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" autocomplete="off" class="layui-input" style="width: 150px">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"><span
                            class="layui-icon layui-icon-search"></span>查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置
                    </button>
                </div>
            </div>
        </div>
    </form>
</blockquote>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="customerTable" lay-filter="customerTable"></table>
    <div id="customerRowBar" style="display: none;" sec:authorize="hasPermission('/admin/user/banCustomer','customer:ban')">
        {{#  if(d.available == 1){ }}
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>禁用</button>
        {{#  } }}
        {{#  if(d.available == 0){ }}
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-delete"></span>解禁</button>
        {{#  } }}
    </div>
</div>

<!-- 数据表格结束 -->

<script type="text/javascript" th:src="@{/layui/layui.js}"></script>
<script type="text/javascript">
    var tableIns;
    layui.extend({
        dtree: '/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['jquery', 'form', 'table', 'layer'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        //加载 数据
        tableIns = table.render({
            elem: '#customerTable'
            , url: '/admin/user/loadAllCustomer'
            , toolbar: '#customerToolBar' //开启头部工具栏，并为其绑定左侧模板
            , title: '客户数据表'
            , page: true
            , cols: [ [
                {field: 'name', title: '姓名', align: 'center', width: '120'}
                , {field: 'username', title: '帐号名', align: 'center', width: '160'}
                , {
                    field: 'sex', title: '性别', align: 'center', width: '60', templet: function (d) {
                        return d.sex === 1 ? '<span style="color: blue">男</span>' : '<span style="color: red">女</span>';
                    }
                }
                , {
                    field: 'available', title: '是否禁用', align: 'center', width: '90', templet: function (d) {
                        return d.available == 1 ? '<span style="color: blue">否</span>' : '<span style="color: red">是</span></font>';
                    }
                }
                , {field: 'phone', title: '手机号', align: 'center', width: '120'}
                , {field: 'identity', title: '身份证', align: 'center', width: '180'}
                , {fixed: 'right', title: '操作', toolbar: '#customerRowBar', align: 'center', width: '100'}
            ] ]
            , done: function (res, curr, count) { //处理删除某一页最后一条数据的BUG
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            }
        });

        //模糊查询
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监听行工具条的事件
        table.on("tool(customerTable)", function (obj) {
            var data = obj.data; //获得当前行数据
            switch (obj.event) {
                case 'delete':
                    deleteCustomer(data);
                    break;
            }
            ;
        });
        //删除
        function deleteCustomer(data) {
            layer.confirm('你确定要将【' + data.name + '】这个客户禁用吗?', {icon: 3, title: '提示'}, function (index) {
                $.post("/admin/user/banCustomer", {id: data.id, available: data.available}, function (res) {
                    if (res.code == 200) {
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }
    });
</script>

</body>
</html>